package com.kuaimacode.kframework.util;

import com.aliyuncs.CommonRequest;
import com.aliyuncs.CommonResponse;
import com.aliyuncs.DefaultAcsClient;
import com.aliyuncs.IAcsClient;
import com.aliyuncs.dyvmsapi.model.v20170525.ClickToDialRequest;
import com.aliyuncs.dyvmsapi.model.v20170525.ClickToDialResponse;
import com.aliyuncs.exceptions.ClientException;
import com.aliyuncs.exceptions.ServerException;
import com.aliyuncs.http.MethodType;
import com.aliyuncs.profile.DefaultProfile;
import com.aliyuncs.profile.IClientProfile;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;


/**
 * 阿里云语音通知接口
 */
public class AlipayVoiceUtil {

    /**
     * 日志
     * @return
     */
    public static Logger getLogger() {
        Logger logger = LoggerFactory.getLogger("AlipayVoiceUtil");
        return logger;
    }
    //private final Logger logger = LoggerFactory.getLogger(this.getClass());

    /**
     * 点击拨号接口
     * @param phoneNo 被叫号码
     * @return
     */
    public static boolean clickToDial(String accessKeyIds, String accessKeySecrets, String callNumber,
                                      String phoneNo, String dispatchId){
        //设置访问超时时间
        System.setProperty("sun.net.client.defaultConnectTimeout", "10000");
        System.setProperty("sun.net.client.defaultReadTimeout", "10000");
        //云通信产品-语音API服务产品名称（产品名固定，无需修改）
        final String product = "Dyvmsapi";
        //语音API服务产品域名（接口地址固定，无需修改）
        final String domain = "dyvmsapi.aliyuncs.com";
        //需要替换成你的AK信息
        final String accessKeyId = accessKeyIds;
        final String accessKeySecret = accessKeySecrets;
        //初始化acsClient暂时不支持多region（暂时固定，无需修改）
        IClientProfile profile = DefaultProfile.getProfile("cn-hangzhou", accessKeyId, accessKeySecret);

        try {
            DefaultProfile.addEndpoint("cn-hangzhou", "cn-hangzhou", product, domain);
        } catch (ClientException e) {
            //e.printStackTrace();
        }
        IAcsClient acsClient = new DefaultAcsClient(profile);
        //组装请求对象-具体描述见控制台-文档部分内容
        ClickToDialRequest request = new ClickToDialRequest();
        //必填-主叫显号,可在语音控制台中找到所购买的显号
        request.setCallerShowNumber(callNumber);
        //必填-主叫号码
        request.setCallerNumber(callNumber);
        //必填-被叫显号,可在语音控制台中找到所购买的显号
        request.setCalledShowNumber(phoneNo);
        //必填-被叫号码
        request.setCalledNumber(phoneNo);
        //可选-外部扩展字段
        request.setOutId(dispatchId);
        //hint 此处可能会抛出异常，注意catch
        ClickToDialResponse clickToDialResponse = null;
        try {
            clickToDialResponse = acsClient.getAcsResponse(request);
        } catch (ClientException e) {
            e.printStackTrace();
        }
        if(clickToDialResponse.getCode()!=null && clickToDialResponse.getCode().equals("OK")) {
            getLogger().debug("语音拨号成功");
            //请求成功
            return true;
        }else{
            getLogger().error("语音拨号失败"+":"+clickToDialResponse.getCode()+","+ clickToDialResponse.getMessage());
        }
        return false;


        /*DefaultProfile profile = DefaultProfile.getProfile("cn-hangzhou", "<accessKeyId>", "<accessSecret>");
        IAcsClient client = new DefaultAcsClient(profile);

        CommonRequest request = new CommonRequest();
        request.setMethod(MethodType.POST);
        request.setDomain("dyvmsapi.aliyuncs.com");
        request.setVersion("2017-05-25");
        request.setAction("ClickToDial");
        request.putQueryParameter("RegionId", "cn-hangzhou");
        request.putQueryParameter("CallerShowNumber", "073182705883");
        request.putQueryParameter("CallerNumber", "073182705883");
        request.putQueryParameter("CalledShowNumber", "17602180207");
        request.putQueryParameter("CalledNumber", "17602180207");
        request.putQueryParameter("RecordFlag", "false");
        request.putQueryParameter("AsrFlag", "false");
        request.putQueryParameter("SessionTimeout", "100");
        request.putQueryParameter("OutId", "test01");
        try {
            CommonResponse response = client.getCommonResponse(request);
            System.out.println(response.getData());
        } catch (ServerException e) {
            e.printStackTrace();
        } catch (ClientException e) {
            e.printStackTrace();
        }*/
    }

    /**
     * 文本转语音
     * @param accessKeyIds
     * @param accessKeySecrets
     * @param callNumber
     * @param phoneNo
     * @param ttsCode
     * @param dispatchId
     * @return
     */
    /*public static boolean singleCallByTts(String accessKeyIds, String accessKeySecrets, String callNumber,
                                          String phoneNo, String ttsCode, String dispatchId){
        //设置访问超时时间
        System.setProperty("sun.net.client.defaultConnectTimeout", "10000");
        System.setProperty("sun.net.client.defaultReadTimeout", "10000");
        //云通信产品-语音API服务产品名称（产品名固定，无需修改）
        final String product = "Dyvmsapi";
        //产品域名（接口地址固定，无需修改）
        final String domain = "dyvmsapi.aliyuncs.com";
        //AK信息
        final String accessKeyId = accessKeyIds;
        final String accessKeySecret = accessKeySecrets;
        //初始化acsClient 暂时不支持多region
        IClientProfile profile = DefaultProfile.getProfile("cn-hangzhou", accessKeyId, accessKeySecret);
        try {
            DefaultProfile.addEndpoint("cn-hangzhou", "cn-hangzhou", product, domain);
        } catch (ClientException e) {
            e.printStackTrace();
        }
        IAcsClient acsClient = new DefaultAcsClient(profile);
        SingleCallByTtsRequest request = new SingleCallByTtsRequest();
        //必填-被叫显号,可在语音控制台中找到所购买的显号
        request.setCalledShowNumber(callNumber);
        //必填-被叫号码
        request.setCalledNumber(phoneNo);
        //必填-Tts模板ID
        request.setTtsCode(ttsCode);
        //可选-当模板中存在变量时需要设置此值
        //request.setTtsParam("{\"code\":\"123456\"}");
        //可选-音量 取值范围 0--200
        request.setVolume(80);
        //可选-播放次数
        request.setPlayTimes(3);
        //可选-外部扩展字段,此ID将在回执消息中带回给调用方
        request.setOutId(dispatchId);
        //hint 此处可能会抛出异常，注意catch
        SingleCallByTtsResponse singleCallByTtsResponse = null;
        try {
            singleCallByTtsResponse = acsClient.getAcsResponse(request);
        } catch (ClientException e) {
            e.printStackTrace();
        }
        if(singleCallByTtsResponse.getCode() != null && singleCallByTtsResponse.getCode().equals("OK")) {
            //请求成功
            System.out.println("语音文本外呼---------------");
            System.out.println("RequestId=" + singleCallByTtsResponse.getRequestId());
            System.out.println("Code=" + singleCallByTtsResponse.getCode());
            System.out.println("Message=" + singleCallByTtsResponse.getMessage());
            System.out.println("CallId=" + singleCallByTtsResponse.getCallId());
            return true;
        }
        return false;
    }*/


    /**
     * 文本转语音
     * @param accessKeyIds
     * @param accessKeySecrets
     * @param callNumber
     * @param phoneNo
     * @param ttsCode
     * @param dispatchId
     * @return
     */
    public static boolean singleCallByTts(String accessKeyIds, String accessKeySecrets, String callNumber,
                                          String phoneNo, String ttsCode, String dispatchId){
        //设置访问超时时间
        System.setProperty("sun.net.client.defaultConnectTimeout", "10000");
        System.setProperty("sun.net.client.defaultReadTimeout", "10000");
        DefaultProfile profile = DefaultProfile.getProfile("cn-hangzhou", accessKeyIds, accessKeySecrets);
        IAcsClient client = new DefaultAcsClient(profile);

        CommonRequest request = new CommonRequest();
        request.setMethod(MethodType.POST);
        request.setDomain("dyvmsapi.aliyuncs.com");
        request.setVersion("2017-05-25");
        request.setAction("SingleCallByTts");
        request.putQueryParameter("RegionId", "cn-hangzhou");
        request.putQueryParameter("CalledShowNumber", callNumber);

        request.putQueryParameter("CalledNumber", phoneNo);
        request.putQueryParameter("TtsCode", ttsCode);
        request.putQueryParameter("OutId", dispatchId);
        try {
            CommonResponse response = client.getCommonResponse(request);
            System.out.println(response.getData());
            return true;
        } catch (ServerException e) {
            e.printStackTrace();
        } catch (ClientException e) {
            e.printStackTrace();
        }
        return false;
    }


    public static void main(String[] args) {
        //clickToDial("LTAIPS2N069By0Cz", "ibyMPpDT0ycA6Q3HwTH79LjzMZEkf8",
         //       "073182705883", "17602180207", "test01");
       /* clickToDial("LTAIeLbo2JnRkHI6", "ECyZ0K7P4g8dNiKMtdFbHTqFcFpdho",
                "073182705883", "17602180207", "test01");*/
       singleCallByTts("LTAIPS2N069By0Cz", "ibyMPpDT0ycA6Q3HwTH79LjzMZEkf8",
                       "07502060883", "18825204205","TTS_170156484", "test01");


    }

}
